package com.nba.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nba.bean.Schedule;
import com.nba.service.GameService;
import com.nba.service.PlayerService;
import com.nba.service.TeamService;
import com.nba.service.impl.GameServiceImpl;
import com.nba.service.impl.PlayerServiceImpl;
import com.nba.service.impl.TeamServiceImpl;

@WebServlet("/gameServlet.do")
public class GameServlet extends HttpServlet{
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}


	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String opt = request.getParameter("opt");
		switch(opt) {
			case "0":
				delete(request,response);
				break;
			case "1":
				add(request,response);
				break;
			case "2":
				deleteAll(request,response);
				break;
			case "3":
				update(request,response);
				break;
			case "4":
				search(request,response);
				break;
		}
	}
	
	public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		GameService gs = new GameServiceImpl();
		int gameId = Integer.parseInt(request.getParameter("scheduleId"));
		gs.deleteSchedule(gameId);
		response.sendRedirect("SearchServlet.do");
	}
	public void deleteAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
		GameService gs = new GameServiceImpl();
		String ids = request.getParameter("ids");
		if(ids!=null) {
			String[] idArray = ids.split("_");
			for (int i = 0; i < idArray.length; i++) {
				gs.deleteSchedule(Integer.parseInt(idArray[i]));
			}
		}
	}
	public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		int gameId = Integer.parseInt(req.getParameter("gameId"));
		int seasonId = Integer.parseInt(req.getParameter("season"));
		String ateamName = req.getParameter("ateamName");
		String bteamName = req.getParameter("bteamName");
		double afirstNum = Double.parseDouble(req.getParameter("afirstNum"));
		double bfirstNum = Double.parseDouble(req.getParameter("bfirstNum"));
		double asecondNum = Double.parseDouble(req.getParameter("asecondNum"));
		double bsecondNum = Double.parseDouble(req.getParameter("bsecondNum"));
		double athirdNum = Double.parseDouble(req.getParameter("athirdNum"));
		double bthirdNum = Double.parseDouble(req.getParameter("bthirdNum"));
		double afourthNum = Double.parseDouble(req.getParameter("afourthNum"));
		double bfourthNum = Double.parseDouble(req.getParameter("bfourthNum"));
		double afastbreakScore = Double.parseDouble(req.getParameter("afastbreakScore"));
		double bfastbreakScore = Double.parseDouble(req.getParameter("bfastbreakScore"));
		double arestrictedScore = Double.parseDouble(req.getParameter("arestrictedScore"));
		double brestrictedScore = Double.parseDouble(req.getParameter("brestrictedScore"));
		double afaultScore = Double.parseDouble(req.getParameter("afaultScore"));
		double bfaultScore = Double.parseDouble(req.getParameter("bfaultScore"));
		String field = req.getParameter("field");
		String str = req.getParameter("scheduleTime").replace("T", " ");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date scheduleTime = null;
		try {
			scheduleTime = format.parse(str);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		TeamService ts  = new TeamServiceImpl();
		int ateamId = ts.selectTeamByName(ateamName).getTeamId();
		int bteamId = ts.selectTeamByName(bteamName).getTeamId();
		Schedule schedule = new Schedule(gameId, seasonId, ateamId, bteamId, afirstNum, bfirstNum, asecondNum, bsecondNum, athirdNum, bthirdNum, afourthNum, bfourthNum, afastbreakScore, bfastbreakScore, arestrictedScore, brestrictedScore, afaultScore, bfaultScore, field, scheduleTime);
		GameService gs = new GameServiceImpl();
		gs.updateSchedule(schedule);
		resp.sendRedirect("SearchServlet.do");
	}
	public void search(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String keyword =request.getParameter("Keyword");
		System.out.println(keyword);
		int pageNum,pageSize;
		if(keyword == null) {
			keyword = "";
		}
		if(request.getParameter("pageNum") == null) {
			pageNum = 1;
		}else {
			pageNum = Integer.parseInt(request.getParameter("pageNum"));
		}
		
		if(request.getParameter("pageSize") == null) {
			pageSize = 10;
		}else {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		GameService gs = new GameServiceImpl();
		request.setAttribute("game", gs.getScheduleAll(keyword, pageNum, pageSize));
		response.sendRedirect("bghome.jsp");
	}
	public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		int seasonId = Integer.parseInt(req.getParameter("season"));
		String ateamName = req.getParameter("ateamName");
		String bteamName = req.getParameter("bteamName");
		double afirstNum = Double.parseDouble(req.getParameter("afirstNum"));
		double bfirstNum = Double.parseDouble(req.getParameter("bfirstNum"));
		double asecondNum = Double.parseDouble(req.getParameter("asecondNum"));
		double bsecondNum = Double.parseDouble(req.getParameter("bsecondNum"));
		double athirdNum = Double.parseDouble(req.getParameter("athirdNum"));
		double bthirdNum = Double.parseDouble(req.getParameter("bthirdNum"));
		double afourthNum = Double.parseDouble(req.getParameter("afourthNum"));
		double bfourthNum = Double.parseDouble(req.getParameter("bfourthNum"));
		double afastbreakScore = Double.parseDouble(req.getParameter("afastbreakScore"));
		double bfastbreakScore = Double.parseDouble(req.getParameter("bfastbreakScore"));
		double arestrictedScore = Double.parseDouble(req.getParameter("arestrictedScore"));
		double brestrictedScore = Double.parseDouble(req.getParameter("brestrictedScore"));
		double afaultScore = Double.parseDouble(req.getParameter("afaultScore"));
		double bfaultScore = Double.parseDouble(req.getParameter("bfaultScore"));
		String field = req.getParameter("field");
		String str = req.getParameter("scheduleTime").replace("T", " ");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date scheduleTime = null;
		try {
			scheduleTime = format.parse(str);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		TeamService ts  = new TeamServiceImpl();
		int ateamId = ts.selectTeamByName(ateamName).getTeamId();
		int bteamId = ts.selectTeamByName(bteamName).getTeamId();
		Schedule schedule = new Schedule( seasonId, ateamId, bteamId, afirstNum, bfirstNum, asecondNum, bsecondNum, athirdNum, bthirdNum, afourthNum, bfourthNum, afastbreakScore, bfastbreakScore, arestrictedScore, brestrictedScore, afaultScore, bfaultScore, field, scheduleTime);
		GameService gs = new GameServiceImpl();
		gs.addSchedule(schedule);
		resp.sendRedirect("SearchServlet.do");
	}
}

